const { model, Sequelize } = require('../../core/model.js');
const md5 = require('md5');

const usersModel = model.define('users', {
  username: {//用户名
    type: Sequelize.STRING(50),
    allowNull: false,
    unique: 'usernameUq',
    validate: {
      is: {
        args: [/^.{4,18}$/],
        msg: '用户名必须4-18个字符',
      }
    }
  },
  password: {//密码
    type: Sequelize.CHAR(32),
    allowNull: false,
    validate: {
      is: {
        args: [/^.{6,18}$/],
        msg: '密码必须6-18个字符',
      }
    }
  },
  email: {//邮箱
    type: Sequelize.STRING(50),
    allowNull: false,
    unique: true,
    validate: {
      isEmail: {
        args: [true],
        msg: '邮箱格式不正确',
      }
    }
  },
  phone: {//手机
    type: Sequelize.CHAR(11),
    allowNull: false,
    unique: true,
    validate: {
      isPhone(value) {
        if (!/^((1[3-8][0-9])+\d{8})$/.test(value)) {
          throw new Error('手机格式错误');
        }
      }
    }
  },
  avatar:{//头像
    type:Sequelize.STRING,
    defaultValue:''
  },
  gender: {//性别
    type: Sequelize.ENUM('m', 'f'),
    allowNull: false,
    // get(value) {
    //   return ({ m: '男', f: '女' })[this.getDataValue('gender')];
    // },
    validate: {
      isIn: {
        args: [['m', 'f']],
        msg: '性别只能取值m（男）或f(女)'
      }
    }
  },
  ctime: Sequelize.DATE,
  mtime: Sequelize.DATE,
}, {
    createdAt: 'ctime',
    updatedAt: 'mtime',
  })
  usersModel.sync({
    // force: true
  });


module.exports = {
  usersModel,
  model
};